import UserModel from "../../schema/user";
import type Typings from "../../typings";

// Mongoose 类的实例
import MongooseController from "../mongoose/mongoose";
const MongooseIns = new MongooseController(UserModel);

// response 响应类型类的实例
import ResController from "../res/res";
const ResIns = new ResController();

class UserController {
  constructor() {}

  // 查看用户列表
  async list(req: Typings.Express.Request, res: Typings.Express.Response) {
    try {
      let { page = 1, size = 10, name = "" } = req.body || {};
      page = Number(page);
      size = Number(size);

      const query: { name?: any } = {};

      if (name) {
        query.name = { $regex: name, $options: "i" };
      }

      const total = await MongooseIns.Count(query);
      const users: Typings.User.props[] = await MongooseIns.Find(query)
        .skip(size * (page - 1))
        .limit(size)
        .sort({
          createdTime: -1,
        });

      const data = {
        page,
        size,
        list: users,
        total,
      };

      ResIns.Success(res, data);
    } catch (err) {
      ResIns.Database(res, err);
    }
  }

  // 查看单个用户
  async check(req: Typings.Express.Request, res: Typings.Express.Response) {
    try {
      const { id } = req.query || {};

      if (!id) {
        ResIns.paramsWrong(res);
        return;
      }

      // 查询用户数据
      const users: Typings.User.props[] = await MongooseIns.Find({
        id,
      });

      if (!users.length) {
        // 用户不存在
        ResIns.isNotExist(res);
        return;
      }

      // 用户存在时
      ResIns.Success(res, users[0]);
    } catch (err) {
      ResIns.Database(res, err);
    }
  }

  // 新增用户
  async add(req: Typings.Express.Request, res: Typings.Express.Response) {
    try {
      const { account, password, name, gender, roleId, avatar, desc } =
        req.body || {};

      const users: Typings.User.props[] = await MongooseIns.Find({
        account,
      });

      if (users.length) {
        ResIns.isExist(res);
        return;
      }

      await MongooseIns.SaveOne(req, {
        account,
        password,
        name,
        gender,
        roleId,
        avatar,
        desc,
      });

      ResIns.Success(res);
    } catch (err) {
      ResIns.Database(res, err);
    }
  }

  // 更新用户信息
  async update(req: Typings.Express.Request, res: Typings.Express.Response) {
    try {
      const { id, password, name, gender, roleId, avatar, desc } =
        <Partial<Typings.User.props>>req.body || {};

      if (!id) {
        ResIns.paramsWrong(res);
        return;
      }

      const updateContent = {
        password,
        name,
        gender,
        roleId,
        avatar,
        desc,
      };

      await MongooseIns.UpdateOne({ id }, updateContent);
      ResIns.Success(res);
    } catch (err) {
      ResIns.Database(res, err);
    }
  }

  // 删除单个用户
  async remove(req: Typings.Express.Request, res: Typings.Express.Response) {
    try {
      let { id } = req.query || {};

      if (!id) {
        ResIns.paramsWrong(res);
        return;
      }

      // 删除用户数据
      await MongooseIns.RemoveOne({ id });

      ResIns.Success(res);
    } catch (err) {
      ResIns.Database(res, err);
    }
  }
}

export default UserController;
